package com.shiguo365.market.display.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shiguo365.market.R;
import com.shiguo365.market.presenter.contract.IOrderPresenter;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/12.
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder>{

    private String TAG="OrderAdapter";

    @BindView(R.id.imageView5) protected ImageView mImageView5;
    @BindView(R.id.imageView6) protected ImageView mImageView6;


    List<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
    IOrderPresenter iOrderPresenter;

    public OrderAdapter(IOrderPresenter iOrderPresenter,List<JSONObject> jsonObjectList) {
        this.jsonObjectList = jsonObjectList;
        this.iOrderPresenter =iOrderPresenter;
    }


    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_order,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        Log.d(TAG,jsonObjectList.get(position).toString());
        viewHolder.update(jsonObjectList.get(position));

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
                    return false;
                }
            });
        }

    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return jsonObjectList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public  class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_amount;
        public TextView tv_orderid;
        public TextView tv_status;
        public TextView tv_paytime;
        public ImageView mImageView4;
        public ImageView mImageView5;
        public ImageView mImageView6;

        JSONObject object;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this, itemView);
            tv_amount = (TextView) view.findViewById(R.id.user_order_item_amount);
            tv_orderid = (TextView) view.findViewById(R.id.user_order_item_id);
            tv_status = (TextView) view.findViewById(R.id.user_order_item_status);
            tv_paytime = (TextView) view.findViewById(R.id.user_order_item_paytime);
            mImageView4 = (ImageView) view.findViewById(R.id.imageViewOrder4);
            mImageView5 = (ImageView) view.findViewById(R.id.imageViewOrder5);
            mImageView6 = (ImageView) view.findViewById(R.id.imageViewOrder6);
        }

        public void update(JSONObject object){

            this.object=object;

            String amount = object.getString("amount");
            int status = object.getIntValue("status");
            String status_text = "";
            if(status==1000){
                status_text = "待受理";
            }else if(status==2000){
                status_text = "待完成";
            }else if(status==3000){
                status_text = "已完成";
            }else if(status==4000){
                status_text = "平台撤消";
            }else if(status==-1){
                status_text = "用户撤消";
            }


            String orderid = object.getString("orderid");
            BigInteger createAt = object.getBigInteger("createAt");




            JSONArray array = object.getJSONArray("prolist");
            ImageLoader imageLoader = ImageLoader.getInstance();



            if(array.size()==1){
                JSONObject jo1 = (JSONObject) array.get(0);
                String picurlarray1 = jo1.getString("picurlarray").replace("[","").replace("]","");
                String image1 = picurlarray1.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image1, mImageView4);
            }

            if(array.size()==2){
                JSONObject jo1 = (JSONObject) array.get(0);
                String picurlarray1 = jo1.getString("picurlarray").replace("[","").replace("]","");
                String image1 = picurlarray1.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image1, mImageView4);

                JSONObject jo2 = (JSONObject) array.get(1);
                String picurlarray2 = jo2.getString("picurlarray").replace("[","").replace("]","");
                String image2 = picurlarray2.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image2, mImageView5);
            }

            if(array.size()>=3){
                JSONObject jo1 = (JSONObject) array.get(0);
                String picurlarray1 = jo1.getString("picurlarray").replace("[","").replace("]","");
                String image1 = picurlarray1.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image1, mImageView4);

                JSONObject jo2 = (JSONObject) array.get(1);
                String picurlarray2 = jo2.getString("picurlarray").replace("[","").replace("]","");
                String image2 = picurlarray2.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image2, mImageView5);

                JSONObject jo3 = (JSONObject) array.get(2);
                String picurlarray3 = jo3.getString("picurlarray").replace("[","").replace("]","");
                String image3 = picurlarray3.split(",")[0].replace("\\","").replace("\"","");
                imageLoader.displayImage(image3, mImageView6);
            }



            Long timestamp = Long.parseLong(createAt.toString())*1000;
            String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));

            this.tv_amount.setText("￥"+amount);
            this.tv_orderid.setText(orderid);
            this.tv_status.setText(status_text);
            this.tv_paytime.setText(date);
//            this.tv_status.setText(status);



        }
    }
}
